package com.tanguy.driver.ui.window;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;

import com.tanguy.driver.R;

import java.util.Date;

/**
 * Created by dab on 2017/9/4 0004 15:21
 */

public class DatePickDialog extends DialogFragment {
    private DatePicker mDatePicker;
    private TextView mTvCancel;
    private TextView mTvSure;
    private long min = 0, max = new Date().getTime();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.dialog_date_pick, container, false);
        mDatePicker = inflate.findViewById(R.id.mDatePicker);
        mTvCancel = inflate.findViewById(R.id.action_cancel);
        mTvSure = inflate.findViewById(R.id.action_sure);
        return inflate;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (max > 0) {
                mDatePicker.setMaxDate(max);
            }
            if (min > 0) {
                mDatePicker.setMinDate(min);
            }
        }
        mTvCancel.setOnClickListener(view1 -> dismiss());
        mTvSure.setOnClickListener(view1 -> {
            if (onDateSetListener != null) {
                onDateSetListener.onSet(mDatePicker.getYear(), mDatePicker.getMonth() + 1, mDatePicker.getDayOfMonth());
                dismiss();
            }
        });
    }

    public void setOnDateSetListener(OnDateSetListener onDateSetListener) {
        this.onDateSetListener = onDateSetListener;
    }

    private OnDateSetListener onDateSetListener;

    public interface OnDateSetListener {
        void onSet(int year, int month, int day);
    }
}
